package com.github.controller;

import com.github.bean.ServerUser;
import com.github.bean.User;
import com.github.bean.UserStatus;
import com.github.manager.Manager;
import com.github.service.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api/status")
public class StatusController {
    @Autowired
    private UserServer userServer;

    @GetMapping("/online-users")
    public Map<String, Object> getOnlineUsers(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        // 获取所有在线用户
        List<UserStatus> allUsers = getAllUsers(true);
        long onlineUsersCount = allUsers.size();

        // 获取所有用户总数
        long allUsersCount = getAllUsers(false).size();

        // 分页处理
        int start = page * size;
        int end = Math.min(start + size, allUsers.size());
        List<UserStatus> pageUsers = allUsers.subList(start, end);

        // 构造分页对象
        Page<UserStatus> userPage = new PageImpl<>(pageUsers, PageRequest.of(page, size), allUsers.size());

        // 构造返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("content", userPage.getContent());
        response.put("totalPages", userPage.getTotalPages());
        response.put("totalElements", userPage.getTotalElements());
        response.put("onlineUsersCount", onlineUsersCount); // 在线用户数
        response.put("allUsersCount", allUsersCount);       // 总用户数

        return response;
    }

    @GetMapping("/all-users")
    public Map<String, Object> getAllUsers(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        // 获取所有用户
        List<UserStatus> allUsers = getAllUsers(false);
        long allUsersCount = allUsers.size();

        // 获取所有在线用户总数
        long onlineUsersCount = getAllUsers(true).size();

        // 分页处理
        int start = page * size;
        int end = Math.min(start + size, allUsers.size());
        List<UserStatus> pageUsers = allUsers.subList(start, end);

        // 构造分页对象
        Page<UserStatus> userPage = new PageImpl<>(pageUsers, PageRequest.of(page, size), allUsers.size());

        // 构造返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("content", userPage.getContent());
        response.put("totalPages", userPage.getTotalPages());
        response.put("totalElements", userPage.getTotalElements());
        response.put("onlineUsersCount", onlineUsersCount); // 在线用户数
        response.put("allUsersCount", allUsersCount);       // 总用户数

        return response;
    }

    private List<UserStatus> getAllUsers(boolean online) {
        List<UserStatus> users = new ArrayList<>();
        for (User user: userServer.getAllUser()) {
            ServerUser serverUser;
            if ((serverUser = Manager.accountUserMap.getOrDefault(user.getAccount(), null)) == null) {
                if (online) {
                    continue;
                }
                users.add(new UserStatus(user.getAccount(), user.getPassword(), false, false, false));
                continue;
            }
            boolean serverFlag = serverUser.isServerOnline();
            boolean cardFlag = serverUser.isCardOnline();
            boolean voiceFlag = serverUser.isVoiceOnline();
            if (online) {
                if (!serverFlag && !cardFlag && !voiceFlag) {
                    continue;
                }

            }
            users.add(new UserStatus(serverUser.getAccount(), user.getPassword(), serverFlag, cardFlag, voiceFlag));
        }
        users.sort(Comparator.comparingInt(user -> Integer.parseInt(user.getUsername())));
        return users;
    }
}